package jm.nj.demo03mian.demo10;

import java.util.ArrayList;

/*
 * instanceof关键字,用于引用数据类型判断
 * 作用:判断某个对象是否属于某种数据类型
 * 前提:对象与数据类型之间必须有继承或者实现关系
 *
 * 格式:
 *       boolean b = 对象 instanceof 数据类型;
 * */
public class Demo {
    public static void main(String[] args) {
        Animal a = new Cat();

        boolean b1 = a instanceof Cat;
        //true
        System.out.println("b1:" + b1);

        boolean b2 = a instanceof Dog;
        //false
        System.out.println("b2:" + b2);

        ArrayList<String> list = new ArrayList<String>();
        //前提:对象和数据之间必须有继承或者实现关系
        //boolean b3 = list instanceof Dog;

        //调用show方法
        show(a);
        show(new Dog());

    }

    private static void show(Animal a) {
        a.eat();
        a.sleep();
        if (a instanceof Cat){
            Cat c = (Cat)a;
            c.catchMouse();
        }
        if (a instanceof Dog){
            Dog d = (Dog)a;
            d.lookHome();
        }
    }
}
